<template>
  <div class="article-comments">
    <!-- 评论列表 -->
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <van-cell
        v-for="(item,index) in list"
        :key="index"
      >
        <van-image
          slot="icon"
          round
          width="30"
          height="30"
          style="margin-right: 10px;"
          :src="item.aut_photo"
        />
        <span style="color: #466b9d;" slot="title">{{item.aut_name}}</span>
        <div slot="label">
          <p style="color: #363636;">{{item.content}}</p>
          <p>
            <span style="margin-right: 10px;">{{item.pubdate | relative}}</span>
            <van-button @click="clickShowReply(item)" size="mini" type="default">回复</van-button>
          </p>
        </div>
        <van-icon @click="toggleCommentLike(item)" slot="right-icon" :name="item.is_liking ? 'like' : 'like-o'" />
      </van-cell>
    </van-list>
    <!-- 评论列表 -->

    <!-- 发布评论 -->
    <van-cell-group class="publish-wrap">
      <van-field
        clearable
        v-model.trim="commentInfo"
        placeholder="请输入评论内容"
        @keyup.enter="issue"
      >
        <van-button @click="issue" slot="button" size="mini" type="info">发布</van-button>
      </van-field>
    </van-cell-group>
    <!-- /发布评论 -->
    <van-popup v-model="isShowReply" round position="bottom" :style="{ height: '85%' }">
      <ReplyComment v-if="isShowReply" :articleId="articleId" :currentComment="currentComment" @closeReply='isShowReply = false'></ReplyComment>
    </van-popup>
  </div>
</template>

<script>
import { reqGetComments, reqSetComment, reqSetGood, reqDelGood } from '@/api/article'
import ReplyComment from './replyComment.vue'
export default {
  name: 'ArticleComment',
  components: {
    ReplyComment
  },
  data () {
    return {
      list: [], // 评论列表
      loading: false, // 上拉加载更多的 loading
      finished: false, // 是否加载结束
      offset: null,
      commentInfo: '',
      isShowReply: false,
      currentComment: {}
    }
  },
  props: {
    articleId: {
      type: String,
      required: true
    }
  },
  created () {
    this.onLoad()
    console.log(this.articleId)
  },
  methods: {
    async onLoad () {
      console.log(this.articleId, this.offset)
      const { data } = await reqGetComments('a', this.articleId, this.offset)
      console.log('获取评论的返回', data)
      this.offset = data.data.last_id
      const arr = data.data.results
      console.log(arr, '-----')
      this.list = [...this.list, ...arr]
      this.loading = false
      if (arr.length === 0) {
        this.finished = true
      }
    },
    // 发布评论
    async issue () {
      if (this.commentInfo === '') return
      console.log(this.articleId, this.commentInfo)
      const { data } = await reqSetComment(this.articleId, this.commentInfo)
      console.log('评论成功的返回', data)
      console.log(data.data.new_obj)
      // 放本地视图的最上面
      this.list.unshift(data.data.new_obj)
      this.$toast.success('评论成功')
      this.commentInfo = ''
      this.onLoad()
    },
    // 切换点赞
    async toggleCommentLike (comment) {
      try {
        if (comment.is_liking) {
          // 是点赞状态,那就取消点赞
          await reqDelGood(comment.com_id)
          this.$toast.success('取消点赞')
        } else {
          // 是取消点赞的状态，那就是点赞
          await reqSetGood(comment.com_id)
          this.$toast.success('点赞成功')
        }
        // 数据反显
        comment.is_liking = !comment.is_liking
      } catch (error) {
        console.log(error)
        this.$toast.fail('操作失败')
      }
    },
    clickShowReply (comment) {
      this.isShowReply = true
      this.currentComment = comment
      console.log(this.currentComment)
    }
  }
}

</script>

<style scoped lang='scss'>
// 发表评论的区域是固定在下端的
.publish-wrap {
  position: fixed;
  left: 0;
  bottom: 0;
  width: 100%;
}
// 给发表评论区空出地方
.van-list {
  margin-bottom: 45px;
}
</style>
